package cn.itcast.p2p20.fragments;


import android.support.v4.app.Fragment;
import android.view.View;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.itcast.p2p20.R;
import cn.itcast.p2p20.bean.RateData;
import cn.itcast.p2p20.bean.ReteResponse;
import cn.itcast.p2p20.bean.request.QueryRequest;
import cn.itcast.p2p20.net.BaseRequest;
import cn.itcast.p2p20.net.SuccessResultCallback;
import cn.itcast.p2p20.utils.Util;

/**
 * A simple {@link Fragment} subclass.
 */
public class RateFragment extends BaseFragment implements SuccessResultCallback<ReteResponse> {

    @InjectView(R.id.linechart)
    LineChart linechart;

    @Override
    protected void initData() {

        sendRequest(new QueryRequest(mContext, "rate"), ReteResponse.class, this);
    }

    @Override
    protected void initView(View childFragmentView) {

        ButterKnife.inject(this, childFragmentView);
    }

    @Override
    protected String getTitle() {
        return null;
    }

    @Override
    protected int getContentLayoutRes() {
        return R.layout.fragment_rate;
    }

    @Override
    public void onSuccessResult(BaseRequest request, ReteResponse response) {

        RateData rateData = response.data;

        List<String> xVals = new ArrayList<>();
        List<Entry> yVals = new ArrayList<>();
        ArrayList<Integer> colors = new ArrayList<>();
        LineDataSet dataSet = new LineDataSet(yVals, "年化收益");

        List<RateData.RateBean> rateBeanList = rateData.getRate();

        for (int i = 0; i < rateBeanList.size(); i++) {
            RateData.RateBean profitBean = rateBeanList.get(i);
            xVals.add(profitBean.getDate());
            yVals.add(new BarEntry((float) profitBean.getRate(), i));
            colors.add(Util.generaRandomColor(mContext));
        }


        dataSet.setColors(colors);
        dataSet.setCircleRadius(1);
        dataSet.setDrawCubic(false);
        dataSet.setDrawValues(false);
        linechart.animateXY(2000, 2000);
        linechart.setData(new LineData(xVals, dataSet));
    }
}
